package boundarys;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JLabel;

import java.awt.List;

import javax.swing.JTextField;
import javax.swing.JComboBox;

import controllers.MarketingSaleReportController;
import entitys.Customer;
import entitys.Reply;
import entitys.Session;
import enums.Command;

import java.awt.TextArea;
import java.util.ArrayList;

import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class MarketingSaleReportGUI extends AbstractGUI{

	/**
	 * Produce marketing sale report frame
	 */
	private JFrame frmproduceReportmarketingSale;
	/**
	 * number of addressed clients text field
	 */
	private JTextField addressedclientstextField;
	/**
	 * number of sales in the selected marketing sale text field
	 */
	private JTextField soldtextField;
	/**
	 * Labels
	 */
	private JLabel selectSaleLabel;
	private JLabel reviewLabel;
	private JLabel addressedClientsLabel; 
	private JLabel soldProductLabel;
	/**
	 * list of marketing sales to add to the frame list
	 */
	private List list;
	/**
	 * text area for reviews for selected marketing sale
	 */
	private TextArea textArea;
	/**
	 * Buttons
	 */
	private JButton refreshBtn;

	
	/**
	 * Constructor
	 */
	public MarketingSaleReportGUI() {
		initialize();
		initGUI(frmproduceReportmarketingSale);
		frmproduceReportmarketingSale.getContentPane().setLayout(null);
		
		selectSaleLabel = new JLabel("Select Sale:");
		selectSaleLabel.setBounds(10, 11, 106, 26);
		frmproduceReportmarketingSale.getContentPane().add(selectSaleLabel);
		
		list = new List();
		/**
		 * Item Listener for list
		 * when marketing sale is selected is shows the corresponding details: 
		 * amount of addressed customers
		 * amount of sales for the selected marketing sale
		 * reviews for customers who didn't buy 
		 */
		list.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent arg0) {
				MarketingSaleReportController.getAddressedClientsAmount(Integer.parseInt((String)list.getSelectedItem()));
				MarketingSaleReportController.getSoldAmount(Integer.parseInt((String)list.getSelectedItem()));
				MarketingSaleReportController.getReviews(Integer.parseInt((String)list.getSelectedItem()));
				
			}
		});

		list.setBounds(10, 43, 226, 281);
		MarketingSaleReportController.getMarketingSales();
		frmproduceReportmarketingSale.getContentPane().add(list);
		
		addressedClientsLabel = new JLabel("Number of addressed clients:");
		addressedClientsLabel.setBounds(242, 43, 172, 29);
		frmproduceReportmarketingSale.getContentPane().add(addressedClientsLabel);
		
		addressedclientstextField = new JTextField();
		addressedclientstextField.setEditable(false);
		addressedclientstextField.setBounds(420, 47, 86, 20);
		frmproduceReportmarketingSale.getContentPane().add(addressedclientstextField);
		addressedclientstextField.setColumns(10);
		
		soldProductLabel = new JLabel("Number of successfull sales:");
		soldProductLabel.setBounds(242, 83, 168, 20);
		frmproduceReportmarketingSale.getContentPane().add(soldProductLabel);
		
		soldtextField = new JTextField();
		soldtextField.setEditable(false);
		soldtextField.setBounds(420, 83, 86, 20);
		frmproduceReportmarketingSale.getContentPane().add(soldtextField);
		soldtextField.setColumns(10);
		
		textArea = new TextArea();
		textArea.setEditable(false);
		textArea.setBounds(242, 146, 384, 178);
		frmproduceReportmarketingSale.getContentPane().add(textArea);
		
		/**
		 * Action Listener for Refresh Button
		 * Gets updates details from database and shows it in the list
		 */

		refreshBtn = new JButton("Refresh");
		refreshBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				MarketingSaleReportController.getMarketingSales();
				addressedclientstextField.setText("");
				soldtextField.setText("");
				textArea.setText("");
				
			}
		});
		refreshBtn.setBounds(10, 340, 89, 23);
		frmproduceReportmarketingSale.getContentPane().add(refreshBtn);
		
		reviewLabel = new JLabel("Client review:");
		reviewLabel.setBounds(242, 114, 106, 26);
		frmproduceReportmarketingSale.getContentPane().add(reviewLabel);
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frmproduceReportmarketingSale = new JFrame();

		frmproduceReportmarketingSale.setTitle("...Produce Report>Marketing Sale Report");
		frmproduceReportmarketingSale.setResizable(false);
		frmproduceReportmarketingSale.setBounds(100, 100, 655, 433);
		frmproduceReportmarketingSale.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		frmproduceReportmarketingSale.setVisible(true);
	}

	@SuppressWarnings("unchecked")
	@Override
	/**
	 * Reply from server
	 * Does the right action according to the command that was sent from the controller
	 */
	public void getReply(Object r) {
		// TODO Auto-generated method stub
		if(((Reply)r).command.equals(Command.GET_MARKETING_SALES))
		{
			ArrayList<Integer> id= (ArrayList<Integer>) ((Reply)r).result;
			list.removeAll();
			for(int i=0;i<id.size();i++)
				list.add(id.get(i).toString());
		}
		else if(((Reply)r).command.equals(Command.GET_ADDRESSED_CLIENTS_AMOUNT_BY_MARKETING_SALE))
		{
			String count = (String) ((Reply)r).result;
				addressedclientstextField.setText(count);
		}
		else if(((Reply)r).command.equals(Command.GET_SOLD_AMOUNT))
		{
			String count = (String) ((Reply)r).result;
				soldtextField.setText(count);
		}
		
		else if(((Reply)r).command.equals(Command.GET_REVIEW))
		{
			ArrayList<String> review= (ArrayList<String>) ((Reply)r).result;
				textArea.setText("");
			for(int i=0;i<review.size();i++)
			{
				if(i==0)
					textArea.setText(String.format("%s",review.get(i)));
				else
				textArea.setText(String.format("%s\n%s", textArea.getText(), review.get(i)));
			}
			
		}
	}
}
